package com.zhoug.zmui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.zhoug.common.utils.BitmapUtils;
import com.zhoug.common.utils.LogUtils;

import androidx.annotation.Nullable;

/**
 * 装饰视图
 *
 * @Author 35574
 * @Date 2020/7/28
 * @Description
 */
public class DecorRootView extends FrameLayout {
    private static final String TAG = ">>>DecorRootView";
    private static final boolean DEBUG = false;
    private int corners = 50;
    private boolean lt = true;
    private boolean rt = true;
    private boolean rb = true;
    private boolean lb = true;

    private int border = 6;
    private int borderColor = Color.RED;
    private int backgroundColor = Color.GRAY;
    private Paint mPaint;
    private Path mPath;
    private View mContentView;

    private int decorLeft = 30;
    private int decorTop = 100;
    private int decorRight = 30;
    private int decorBottom = 30;

    private Bitmap bitmap;
    private Bitmap arrowBitmap;
    private Canvas canvas;

    public DecorRootView(Context context) {
        this(context, null);
    }

    public DecorRootView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DecorRootView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAntiAlias(true);
    }

    public void setContentView(View contentView) {
        if (mContentView != null) {
            removeView(mContentView);
        }
        if (contentView.getParent() != null) {
            ((ViewGroup) contentView.getParent()).removeView(contentView);
        }
        mContentView = contentView;
        addView(contentView);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        LogUtils.d(TAG, "onSizeChanged:");

        mPath = new Path();
        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitmap);
        path(w - decorLeft - decorRight - border, h - decorTop - decorBottom - border);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setStrokeWidth(border);
        mPaint.setColor(borderColor);
        canvas.save();
        canvas.translate(decorLeft + border / 2, decorTop + border / 2);
        canvas.drawPath(mPath, mPaint);
        canvas.restore();
        arrowBitmap=Bitmap.createBitmap(50,100, Bitmap.Config.ARGB_8888);
        canvas.setBitmap(arrowBitmap);
        Path path=new Path();
        path.moveTo(0,0);
        path.lineTo(arrowBitmap.getWidth()/2,arrowBitmap.getHeight());
        path.lineTo(arrowBitmap.getWidth(),0);
        canvas.drawPath(path, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LogUtils.d(TAG, "onMeasure:");
        measureChildWithMargins(mContentView, widthMeasureSpec, decorLeft + decorRight + border,
                heightMeasureSpec, decorTop + decorBottom + border);
        int contentWidth = mContentView.getMeasuredWidth();
        int contentHeight = mContentView.getMeasuredHeight();

        int width, height;
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);


        if (MeasureSpec.getMode(widthMeasureSpec) == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = Math.min(widthSize, contentWidth + decorLeft + decorRight + border);
        }
        if (MeasureSpec.getMode(heightMeasureSpec) == MeasureSpec.EXACTLY) {
            height = heightSize;
        } else {
            height = Math.min(heightSize, contentHeight + decorTop + decorBottom + border);
        }


        setMeasuredDimension(width, height);
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        LogUtils.d(TAG, "onLayout:");
//        super.onLayout(changed, left+decorLeft+border/2, top+decorTop+border/2,
//                right-decorRight-border/2, bottom-decorBottom-border/2);
        mContentView.layout(left + decorLeft + border / 2, top + decorTop + border / 2,
                right - decorRight - border / 2, bottom - decorBottom - border / 2);

    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        canvas.drawBitmap(bitmap, 0, 0, mPaint);
        canvas.save();
        canvas.translate(decorLeft + border / 2, decorTop + border / 2);
        mPaint.setColor(borderColor);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setXfermode(null);
        canvas.drawPath(mPath,mPaint);
        canvas.restore();
        canvas.save();
//        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        Bitmap mirrorVertical = BitmapUtils.getMirrorVertical(arrowBitmap);
        canvas.drawBitmap(mirrorVertical,(canvas.getWidth()-mirrorVertical.getWidth())/2,0,mPaint);
    }

    private void path(int width, int height) {
        //最大角度
        int maxCorners = Math.min(width, height) / 2;
        if (corners > maxCorners) {
            corners = maxCorners;
        }
        //左上角
        mPath.moveTo(0, corners);
        if (lt) {
            mPath.quadTo(0, 0, corners, 0);
        } else {
            mPath.lineTo(0, 0);
        }

        mPath.lineTo(width - corners, 0);
        //右上角
        if (rt) {
            mPath.quadTo(width, 0, width, corners);
        } else {
            mPath.lineTo(width, 0);
        }
        mPath.lineTo(width, height - corners);
        //右下角
        if (rb) {
            mPath.quadTo(width, height, width - corners, height);
        } else {
            mPath.lineTo(width, height);
        }
        mPath.lineTo(corners, height);
        //左下角
        if (lb) {
            mPath.quadTo(0, height, 0, height - corners);
        } else {
            mPath.lineTo(0, height);
        }
        mPath.lineTo(0, corners);


    }
}
